package io.renren.modules.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.service.dao.ProductDao;
import io.renren.modules.service.entity.ProductCodeEntity;
import io.renren.modules.service.entity.ProductEntity;
import io.renren.modules.service.service.MobileWorkerService;
import io.renren.modules.service.service.ProductCodeService;
import io.renren.modules.service.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("productService")
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Autowired
    private MobileWorkerService mobileWorkerService;
    @Autowired
    private ProductCodeService productCodeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        String number = (String) params.get("number");
        String workerName = (String) params.get("workerName");
        String current = (String) params.get("current");
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
                        .eq(StringUtils.isNotBlank(name), "name", name)
                        .eq(StringUtils.isNotBlank(number), "number", number)
                        .eq(StringUtils.isNotBlank(workerName), "worker_name", workerName)
                        .eq(StringUtils.isNotBlank(current), "current", current)
                        .orderByDesc("id")
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insert(String productList) {
        List<ProductEntity> productEntityList = JSON.parseArray(productList, ProductEntity.class);
        if (CollectionUtils.isNotEmpty(productEntityList)) {
            Map<String, Object> map = productCodeService.queryProductNameAndNumber();
            for (ProductEntity entity : productEntityList) {
                if (entity.getDeviceId() != null) {
                    String workerName = mobileWorkerService.queryWorkerNameByDeviceId(entity.getDeviceId());
                    entity.setWorkerName(workerName);
                    if (MapUtils.isNotEmpty(map)) {
                        entity.setName((String) map.get("name"));
                        entity.setNumber((String) map.get("number"));
                    }
                }
            }
            this.saveBatch(productEntityList);
            return (String) map.get("name");
        }
        return null;
    }

}